using Microsoft.Practices.Unity;
using UnityDemo.AutomaticFactoryCode.DataModel;
using UnityDemo.LoggingUtility;
using UnityDemo.Models;
using UnityProvider;

namespace UnityDemo.AutomaticFactoryCode.AutomaticFactory
{
    internal class AutomaticFactoryDemoNew : ISample
    {
        public AutomaticFactoryDemoNew()
        {
            LogerInstance.Logger.Initialize(this);
            UnityInstanceProvider.ApplicationWideUnityInstance.RegisterType<IServiceConsumer, ServiceConsumer>()
                .RegisterType<IServiceProvider, ServiceProvider>();
            var instance = UnityInstanceProvider.ApplicationWideUnityInstance.Resolve<IServiceConsumer>();
            instance.DoSomething();
        }

        #region Nested type: ServiceConsumer

        internal class ServiceConsumer : IServiceConsumer
        {
            private readonly System.Func<IServiceProvider> _resourceFactory;
            private IServiceProvider _resource;

            public ServiceConsumer(System.Func<IServiceProvider> resourceFactory)
            {
                _resourceFactory = resourceFactory;
                LogerInstance.Logger.Initialize(this);
                _resourceFactory = resourceFactory;
            }

            private IServiceProvider Resource
            {
                get { return _resource ?? (_resource = _resourceFactory()); }
            }

            #region IServiceConsumer Members

            public void DoSomething()
            {
                LogerInstance.Logger.Info(" Started doing something ");
                Resource.GetEmployeeNames();
            }

            public void DoSomethingElse()
            {
                // NB _resource is not used in this method        // ...
            }

            #endregion
        }

        #endregion
    }
}